import 'package:flutter/material.dart';

class SleepGridView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      color: Color(0xFFF5F5F6),
      child: Column(
        children: [
          Row(
            children: [
              Item(title: "睡眠时长", type: 1),
              SizedBox(
                width: 10,
              ),
              Item(title: "打鼾次数", type: 2),
            ],
          ),
          SizedBox(height: 10),
          Row(
            children: [
              Item(title: "气动次数", type: 3),
              SizedBox(
                width: 10,
              ),
              Item(title: "翻身次数", type: 4),
            ],
          ),
        ],
      ),
    );
  }
}

class Item extends StatelessWidget {
  final String title;
  final int type;

  Item({super.key, required this.title, required this.type});

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        padding: const EdgeInsets.all(8),
        decoration: const BoxDecoration(
            //圆角半径
            borderRadius: BorderRadius.all(Radius.circular(12.0)),
            color: Colors.white),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title, style: const TextStyle(color: Colors.black, fontSize: 12)),
            const SizedBox(height: 12),
            Row(children: [
              SizedBox(width: 1),
              Text(getContent(type)[0],
                  style: TextStyle(fontSize: 20, color: Colors.black, fontWeight: FontWeight.bold)),
              SizedBox(width: 1),
              Text(getContent(type)[1], style: TextStyle(fontSize: 12, color: Color(0xFFA1A1AD))),
              SizedBox(width: 1),
              Text(getContent(type)[2],
                  style: TextStyle(fontSize: 20, color: Colors.black, fontWeight: FontWeight.bold)),
              SizedBox(width: 1),
              Text(getContent(type)[3], style: TextStyle(fontSize: 12, color: Color(0xFFA1A1AD))),
            ]),
            Row(
              children: [
                Text(
                  "偏多",
                  style: TextStyle(fontSize: 12, color: getColor(type % 2 == 0)),
                ),
                Text(
                  "较上周↑5次",
                  style: TextStyle(fontSize: 12, color: getColor(type % 2 == 0)),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  List<String> getContent(int type) {
    switch (type) {
      case 1:
        return ["8", "小时", "43", "分钟"];
      case 2:
        return ["75", "次", "", ""];
      case 3:
        return ["47", "次", "", ""];
        case 4:
        return ["12", "次", "", ""];
    }

    return [];
  }

  Color getColor(bool b) {
    Color color = Colors.black;
    if (b) {
      color = Color(0xFF61CB88);
    } else {
      color = Color(0xFFFF6178);
    }
    return color;
  }
}
